// const Mock = require('mockjs')
// import { deepClone } from '../../src/utils/index.js'
// import { asyncRoutes, constantRoutes } from './routes.js'

// const routes = deepClone([...constantRoutes, ...asyncRoutes])

// 路由
const roles = require('./routes')

const rolelist = require('./list')

const tree = require('./tree')

const rightslit = require('./rights')

const users = {
  'admin-token': {
    roleId: 1,
    roles: 'admin',
    introduction: 'I am a super administrator',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: 'Super Admin'
  },
  'editor-token': {
    roleId: 2,
    roles: 'editor',
    introduction: 'I am an editor',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: 'Normal editor'
  },
  'visitor-token': {
    roleId: 3,
    roles: 'visitor',
    introduction: 'I am an visitor',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: 'Normal visitor'
  }
}
// const userlist = require('../userlist')

module.exports = [
  // get user info
  {
    url: '/roles/info\.*',
    type: 'get',
    response: config => {
      const { token } = config.query
      const info = users[token]

      // mock error
      if (!info) {
        return {
          _RejCode: '50008',
          _RejMsg: '登录失败，用户不存在'
        }
      }

      return {
        _RejCode: '00000000',
        _RejMsg: '请求成功',
        data: info
      }
    }
  },
  // mock get all routes form server
  {
    url: '/routes',
    type: 'get',
    response: config => {
      const { roleId } = config.query
      // 先获取用户信息，再根据用户信息获取路由表
      const r = roles.find(item => item.roleId * 1 === roleId * 1)
      let routes
      if (r) routes = r.routes
      return {
        _RejCode: '00000000',
        _RejMsg: '获取路由成功',
        data: routes
      }
    }
  },

  // mock get all roles form server
  {
    url: '/roles',
    type: 'get',
    response: config => {
      const { roleId } = config.query
      if (roleId) {
        const role = rolelist.find(item => item.roleId * 1 === roleId * 1)
        return {
          _RejCode: '00000000',
          _RejMsg: '获取角色信息成功',
          data: role
        }
      }
      return {
        _RejCode: '00000000',
        _RejMsg: '获取角色列表成功',
        data: rolelist
      }
    }
  },

  // add role
  {
    url: '/roles/add',
    type: 'post',
    response: config => {
      const { roleDesc, roleName } = config.body
      const role = {
        roleId: rolelist.length + 1000,
        roleName,
        roleDesc,
        children: []
      }
      rolelist.push(role)
      return {
        _RejCode: '00000000',
        _RejMsg: '创建成功',
        data: role
      }
    }
  },

  // update 角色信息
  {
    url: '/roles/update',
    type: 'put',
    response: config => {
      const { roleName, roleDesc, roleId } = config.body
      let user = {}
      // const roleName = roles.find(item => item.roleId * 1 === roleId * 1).roleName
      rolelist.forEach(element => {
        if (element.roleId * 1 === roleId * 1) {
          element.roleName = roleName
          element.roleDesc = roleDesc
          user = element
        }
      })
      return {
        _RejCode: '00000000',
        _RejMsg: '更新角色成功',
        data: user
      }
    }
  },
  // delete role
  {
    url: '/roles/delete',
    type: 'delete',
    response: config => {
      const { roleId } = config.body
      const index = rolelist.findIndex(item => item.roleId * 1 === roleId * 1)
      rolelist.splice(index, 1)
      return {
        _RejCode: '00000000',
        _RejMsg: '删除角色成功',
        data: null
      }
    }
  },

  // delete role
  {
    url: '/rights/tree',
    type: 'get',
    response: config => {
      return {
        _RejCode: '00000000',
        _RejMsg: '获取权限列表成功',
        data: tree
      }
    }
  },

  // 设置角色权限
  {
    url: '/roles/rights/set',
    type: 'post',
    response: config => {
      // const { roleId, rightsId } = config.body
      // const rights = rightsId.split(',')
      // rolelist.forEach(item => {
      //   if (item.roleId * 1 === roleId * 1) {
      //     item.children = filterRoles(item.children, rights)
      //   }
      // })
      return {
        _RejCode: '00000000',
        _RejMsg: '设置角色权限成功',
        data: rolelist
      }
    }
  },

  // 取消角色权限
  {
    url: '/roles/rights/cancel',
    type: 'delete',
    response: config => {
      // const { roleId, rightId } = config.body
      // rolelist.forEach(item => {
      //   if (item.roleId * 1 === roleId * 1) {
      //     fn(item, rightId)
      //   }
      // })
      return {
        _RejCode: '00000000',
        _RejMsg: '取消角色权限成功',
        data: null
      }
    }
  },

  // 取消角色权限
  {
    url: '/rights/list',
    type: 'get',
    response: config => {
      return {
        _RejCode: '00000000',
        _RejMsg: '权限列表获取成功',
        data: rightslit
      }
    }
  }
]
